package com.xiangxiangjd.controller;

import cn.afterturn.easypoi.word.WordExportUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xiangxiangjd.controller.helper.$;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.xiangxiangjd.bean.result.ResultPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xiangxiangjd.service.ClaimDateNoService;
import com.xiangxiangjd.bean.ClaimDateNo;
import com.xiangxiangjd.controller.model.ApiResultModel;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 申领航材批次号 前端控制器
 *
 * @author 肖守凯
 * @since 2021-11-08
 */
@Controller
@RequestMapping("/api/claimDateNo")
@Api(tags = "申领航材/耗材")
public class ClaimDateNoController extends BaseController {

  /**
   * 分页查询ClaimDateNo
   *
   * @param page 页码
   * @param limit 查几条
   * @param claimDateNo 前端传进的参数
   * @return 返回统一分页信息
   */
  @PostMapping(value = "/findClaimDateNoByPage")
  @ResponseBody
  @ApiOperation(value = "已申领航材（单号为主）/查询", notes = "已申领航材/查询")
  @PreAuthorize("hasRole('ROLE_USER')")
  public ResultPage findClaimDateNoByPage(Integer page, Integer limit, ClaimDateNo claimDateNo) {

    System.out.println(claimDateNo);

    return claimDateNoService.findClaimDateNoByPage(page, limit, claimDateNo);
  }

  /**
   * 航材 申领与交接
   *
   * @param claimDateNo 前端传进的参数
   * @return 返回统一信息
   */
  @ApiOperation(value = "提交申领", notes = "提交申领")
  @PreAuthorize("hasRole('ROLE_USER')")
  @PostMapping(value = "/addClaimDateNo")
  @ResponseBody
  public ApiResultModel saveClaimDateNo(@RequestBody ClaimDateNo claimDateNo) {
    System.out.println("参数解析：" + claimDateNo.toString());

    return claimDateNoService.addClaimDateNo(claimDateNo);
  }

  /**
   * 编辑ClaimDateNo
   *
   * @param claimDateNo 前端传进的参数
   * @return 返回统一信息
   */
  @PostMapping(value = "admin/editClaimDateNo")
  @ResponseBody
  public ApiResultModel editClaimDateNo(ClaimDateNo claimDateNo) {
    return claimDateNoService.editClaimDateNo(claimDateNo);
  }

  /**
   * 删除ClaimDateNo
   *
   * @param ids 前端传进的ID
   * @return 返回统一信息
   */
  @PostMapping(value = "admin/delClaimDateNo")
  @ResponseBody
  public ApiResultModel delClaimDateNo(@RequestParam("datas") ArrayList ids) {

    return claimDateNoService.delClaimDateNos(ids);
  }
  /** 审批待申领物品 审批通过 */
  @PostMapping(value = "/admin/approvalApproved")
  @ResponseBody
  @ApiOperation(value = "航材申领审批通过", notes = "航材申领审批通过")
  @PreAuthorize("hasRole('ROLE_ADMIN')")
  public ApiResultModel approvalApproved(@RequestParam Integer claimDateNoId) {

    return claimDateNoService.approvalApproved(claimDateNoId, getUserInfo().getId());
  }

  /** 查询申领订单下的详细信息 */
  @PostMapping(value = "showApplicationAndReturnDetails")
  @ResponseBody
  @ApiOperation(value = "查询申领订单下的详细信息", notes = "查询申领订单下的详细信息")
  @PreAuthorize("hasRole('ROLE_USER')")
  public ApiResultModel showApplicationAndReturnDetails(
      @RequestParam(value = "claimDateNoId") Integer claimDateNoIde) {

    return claimDateNoService.showApplicationAndReturnDetails(claimDateNoIde);
  }

  /** 审批申领物品不通过 审批通过 */
  @PostMapping(value = "/admin/approvalApprovedFail")
  @ResponseBody
  @ApiOperation(value = "审批申领物品不通过", notes = "审批申领物品不通过")
  @PreAuthorize("hasRole('ROLE_ADMIN')")
  public ApiResultModel approvalApprovedFail(@RequestParam("claimDateNoId") Integer claimDateNoId, String reasonForFailure) {

    //
    boolean update =
        claimDateNoService.update(
            new LambdaUpdateWrapper<ClaimDateNo>()
                .set(ClaimDateNo::getClaimDateNoState, "3")
                .set(ClaimDateNo::getReasonForFailure,reasonForFailure)
                 .set(ClaimDateNo::getClaimApprovalUserId,getUserInfo().getId())
                .eq(ClaimDateNo::getId, claimDateNoId));

    if (!update) {
      return $.error("审批失败");
    }

    return $.success("不通过成功");
  }


  /**
   * 航材申领报告表
   */
  @GetMapping(value = "/improt")
  @ApiOperation(value = "申领word 导出", notes = "申领word 导出")
  //@PreAuthorize("hasRole('ROLE_USER')")
  public void downDocClaimDateNo(@RequestParam("claimDateNoId") Integer claimDateNoId) throws Exception {


    Map map=claimDateNoService.downDocClaimDateNo(claimDateNoId);


    XWPFDocument doc = WordExportUtil.exportWord07("word/ClaimDateNo.docx", map);

    doc.enforceReadonlyProtection();

    String fileName = System.currentTimeMillis() + ".docx";
    HttpServletResponse response = getResponse();

    response.reset();
    response.setContentType("application/msword");
    String dispositionValue = "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8");
    response.setHeader("Content-disposition", dispositionValue);
    response.setCharacterEncoding("UTF-8");

    OutputStream output = response.getOutputStream();
    doc.write(output);
    output.close();

  }



}
